

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var userId = user.id;
    //猜你喜欢获取用户浏览记录最多的和次多的分类id
    var goods_list = JSON.parse(localStorage.getItem('goods_list'));
    var param = {};
    if (goods_list) {
        var cats_id = [];
        for (var i = 0; i < goods_list.length; i++) {
            cats_id.push(goods_list[i].cat_id);
        }
        var maxV = times(cats_id).maxV;
        var secondV = times(cats_id).secondV;
        //猜你喜欢请求接口获取商品数据   
    }
    param.main_cat_id = undefined == maxV ? 0 : maxV;
    param.less_cat_id = undefined == secondV ? 0 : secondV;
    $.ajax({
        type: "post",
        url: host + "/api/goods/guess_like",
        contentType: "application/json",
        xhrFields: {
            withCredentials: true
        },
        data: JSON.stringify(param),
        success: function success(data) {
            if (data.code == 200&& data.data) {
                var data = data.data;
               
                        $('.check_btn').css('display','block')
                        var html = '';
                        for(var i = 0;i<data.length;i++){
                                html+="<li>\n <a href=\"/goods-" + data[i].id + ".html\" target=\"_blank\">\n <img src=\"" + isImgHttp(data[i].thumb) + "\" title=\"" + data[i].name + "\" alt=\"" + data[i].name + "\">\n                                    <span class=\"zh\">" + data[i].name + "</span>\n                                </a>\n                            \n                                <span class=\"last\">\xA5" + data[i].goods_price +'/'+data[i].unitName+ "</span>\n                            </li>";
                        };
                      $('.guess_like').html(html);
                      $('.guess_like').removeClass('clearfix')
                        sjShow();
                $('.check_btn').click(function () {
                    sjShow();
                });
            } else {
                alert('网络加载失败，请稍候再试!');
            }
        }
    });

    //数组中出现的次数最多和第二多的元素
    function times(arr) {
        var i;
        var length = arr.length;
        var hash = []; //使用哈希数组
        for (i = 0; i < length; i++) {
            if (!hash[arr[i]]) hash[arr[i]] = 1; //没有初始化的数组元素为undefined，undefined++为NaN
            else hash[arr[i]]++;
        }
        var max = 0; //最多的次数
        var maxV; //出现最多的元素
        var second = 0; //第二多的次数
        var secondV; //出现第二多的元素
        hash.forEach(function (item, index) {
            //forEach函数会跳过空元素
            if (item > max) {
                second = max;
                secondV = maxV;
                max = item;
                maxV = index; //用索引来保存原数组的值
            } else if (item > second) {
                second = item;
                secondV = index;
            }
        });
        return { maxV: maxV, secondV: secondV };
    }

    function sjShow() {
        $(".guess_like li").hide();
        var showCount = 0;
        if($('.same_pic').width()<1000){
            while (showCount < 5) {
                var id = Math.floor(Math.random() * 20);
                var curDiv = $(".guess_like li").eq(id);
                if (curDiv.css("display") == "none") {
                    curDiv.css("display", "block");
                    showCount++;
                }
            } 
        }else{
            while (showCount < 6) {
                var id = Math.floor(Math.random() * 20);
                var curDiv = $(".guess_like li").eq(id);
                if (curDiv.css("display") == "none") {
                    curDiv.css("display", "block");
                    showCount++;
                }
            } 
        }
       
    }
});